package com.controller;

import com.bean.student_info;
import com.bean.student_infoExample;
import com.bean.voluntary_info;
import com.bean.voluntary_infoExample;
import com.dao.student_infoMapper;
import com.dao.voluntary_infoMapper;
import com.model.UserModel;
import com.utils.Sha1;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.ModelMap;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;
import org.springframework.web.bind.annotation.SessionAttributes;

import java.text.SimpleDateFormat;
import java.util.List;

@Controller
@SessionAttributes("loginUser")
public class InitController {

    @Autowired
    private student_infoMapper studentInfoMapper;
    @Autowired
    private voluntary_infoMapper voluntrayInfoMapper;

    @RequestMapping("/init")
    public String init(){
        return "init";
    }

    @RequestMapping("/check_login")
    @ResponseBody
    public String check_login(String name, String identity, ModelMap map, UserModel userModel){
        student_infoExample example = new student_infoExample();
        student_infoExample.Criteria criteria = example.createCriteria();
        String str = Sha1.jdksha1(identity);
        criteria.andSTU_IDENTITYEqualTo(str);
        List<student_info> student = studentInfoMapper.selectByExample(example);
        if(student.isEmpty()){
            return "false";
        }
        else{
            if (!student.get(0).getSTU_NAME().equals(name)){
                return "false";
            }
            voluntary_infoExample example1 = new voluntary_infoExample();
            voluntary_infoExample.Criteria criteria1 = example1.createCriteria();
            criteria1.andSTU_IDEqualTo(student.get(0).getSTU_ID());
            List<voluntary_info> voluntary_infos = voluntrayInfoMapper.selectByExample(example1);
            String date = "";
            if (!voluntary_infos.isEmpty()){
                userModel.setFlag(1);
                date +=voluntary_infos.get(0).getVOL_TIME().substring(0,10);

            }else {
                userModel.setFlag(0);
            }
            userModel.setStu_name(name);
            userModel.setScore(student.get(0).getSTU_SCORE());
            userModel.setTime(date);
            userModel.setStu_id(student.get(0).getSTU_ID());
            map.put("loginUser",userModel);
            return "true";
        }

    }

    @RequestMapping("/main")
    public String showDetail(){
        return "main";
    }
}
